import java.util.Arrays;

public class TestSort {

    public static void insertSort(int[]array){
        for (int i = 1; i < array.length; i++) {
            int tmp=array[i];
            int j = i-1;
            for (; j >=0 ; j--) {
                if(array[j]>tmp){
                    array[j+1]=array[j];
                }else{
                    break;
                }
            }
            array[j+1]=tmp;
        }
    }

    public static void shell(int[]array,int gap){
        for (int i = 1; i <array.length ; i++) {
            int tmp=array[i];
            int j = i-gap;
            for (; j >=0 ; j-=gap) {
                if(array[j]>tmp){
                    array[j+gap]=array[j];
                }else{
                    break;
                }
            }
            array[j+gap]=tmp;
        }
    }

    public static void shellSort(int []array){
        int gap=array.length;
        while(gap>1){
            shell(array,gap);
            gap/=2;
        }
        shell(array,1);
    }

    //时间复杂度：O（N^2）
    //空间复杂度：O（1）
    //稳定性：不稳定

    public static void selectSort(int[]array){
        for (int i = 0; i < array.length; i++) {
            for (int j = i+1; j <array.length ; j++) {
                if(array[j]<array[i]){
                    int tmp=array[i];
                    array[i]=array[j];
                    array[j]=tmp;
                }
            }
        }
    }



    //时间复杂度：O(N*logN)
    //空间复杂度：O(1)
    //稳定性：不稳定

    public static void heapSort(int[]array){
        createHeap(array);//建堆时间复杂度O(N)
        int end=array.length-1;
        while(end>0){//O(N)
            int tmp=array[0];
            array[0]=array[end];
            array[end]=tmp;
            shiftDown(array,0,end);//向下调整时间复杂度：O（logN）
            end--;
        }
    }
    public static void createHeap(int[]array){
        for(int parent=(array.length-1-1)/2;parent>=0;parent--){
            shiftDown(array,parent,array.length);
        }
    }
    public static void shiftDown(int[]array,int parent,int len){
        int child=2*parent+1;
        while(child<len){
            if(child+1<len&&array[child]<array[child+1]){
                child++;
            }
            if(array[child]>array[parent]){
                int tmp=array[parent];
                array[parent]=array[child];
                array[child]=tmp;
                parent=child;
                child=2*parent+1;
            }else{
                break;
            }
        }
    }


    //时间复杂度：O(N^2)
    //空间复杂度：O(1)
    //稳定性：稳定的
    public void bubbleSort(int[]array){
        for (int i = 0; i < array.length-1; i++) {
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j]>array[j+1]){
                    int tmp=array[j+1];
                    array[j+1]=array[j];
                    array[j]=tmp;
                }
            }
        }
    }





    public static void main(String[] args) {
        int[]array={55,33,22,4,3,5,12};
        //shellSort(array);
        heapSort(array);
        System.out.println(Arrays.toString(array));
    }
}
